package com.fasteat.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasteat.common.entity.Result;
import com.fasteat.mall.entity.ProductSpecification;
import com.fasteat.mall.service.ProductSpecificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hy
 * @since 2021-02-19
 */
@RestController
@CrossOrigin
@Api(description = "商品规格接口")
@RequestMapping("/mall/productSpecification")
public class ProductSpecificationController {
    @Autowired
    private ProductSpecificationService productSpecificationService;

    /**
     * 新增商品规格信息
     * @param productSpecification
     * @return
     */
    @ApiOperation(value = "新增商品规格信息")
    @PostMapping
    public Result save(@RequestBody ProductSpecification productSpecification) {
        if(productSpecification.getProductId().equals(null)) {
            return Result.error("请勿给不存在的商品添加规格");
        }
        boolean result = productSpecificationService.save(productSpecification);
        return result ?
                Result.success("添加成功")
                :
                Result.error("添加失败");
    }

    /**
     * 根据商品规格id删除商品规格
     * @param productSpecificationId
     * @return
     */
    @DeleteMapping(value = "/delete/{productSpecificationId}")
    @ApiOperation(value = "根据商品规格id删除商品规格")
    public Result deleteById(@PathVariable(name = "productSpecificationId", required = true)
                             @ApiParam(value = "productSpecificationId", required = true)
                                     String productSpecificationId) {
        if (productSpecificationId == null) {
            return Result.error("删除失败");
        }
        boolean result = productSpecificationService.removeById(productSpecificationId);
        return result ?
                Result.success("删除成功")
                :
                Result.error("删除失败");
    }

    /**
     * 根据商品规格id修改商品规格
     * @param productSpecification
     * @return
     */
    @ApiOperation(value = "根据商品规格id修改商品规格")
    @PutMapping
    public Result updateProduct(@RequestBody
                                @ApiParam(value = "商品规格", required = true)
                                            ProductSpecification productSpecification) {
        if (StringUtils.isEmpty(productSpecification.getId())) {
            return Result.error("修改失败");
        }
        boolean result = productSpecificationService.updateById(productSpecification);
        return result
                ?
                Result.success("修改成功")
                :
                Result.error("修改失败");
    }

    /**
     * 通过商品ID获取商品规格
     *
     * @return
     */
    @GetMapping("/findByProductId/{productId}")
    @ApiOperation("通过商品ID获取商品规格")
    public Result findByProductId(@PathVariable(name = "productId", required = true) @ApiParam(value = "商品ID", required = true) String productId) {
        if (StringUtils.isEmpty(productId)) {
            return Result.error("查询失败");
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("product_id", productId);
        ProductSpecification one = productSpecificationService.getOne(wrapper);
        int i = 0;
        return Result.success().data(one);
    }

    /**
     * 通过物流ID获取物流信息
     *
     * @param productSpecificationId
     * @return
     */
    @GetMapping("findById/{productSpecificationId}")
    @ApiOperation("通过商品规格ID获取商品规格")
    public Result findById(@PathVariable(name = "productSpecificationId", required = true)
                           @ApiParam(value = "商品规格ID", required = true)
                                   String productSpecificationId) {
        if (StringUtils.isEmpty(productSpecificationId)) {
            return Result.error("查询失败");
        }
        // 从数据库中查询商品的信息
        ProductSpecification productSpecification = productSpecificationService.getById(productSpecificationId);
        return Result.success("查询成功").data(productSpecification);
    }
}

